package com.example.lottery_system.common.utils;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Configuration
public class RedisUtil {

     private  static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
    /**
     * stringRedisTemplate 字符串redis 模版
     * RedisTemplate byte数组redis模版
     */
    @Resource(name = "stringRedisTemplate")
    private  StringRedisTemplate stringRedisTemplate ;



    /***
     * 添加键值对
     * @param key <String>
     * @param value <String>
     * @return
     */
    public  boolean put(String key, String value) {
//        特判一下
        if (!StringUtils.hasLength(key) || !StringUtils.hasLength(value)) {
            return false;
        }
        try {
            stringRedisTemplate.opsForValue().set(key,value);
            return true;
        } catch (Exception e) {
            logger.info("RedisUtil->key:{},value:{} " ,key,value );
            logger.error(",得到的异常为:" + e.getMessage());
            return false;
        }
    }


    /**
     *
     * 设置超时时间
     *
     * @param key
     * @param value
     * @param outTime 单位(分钟)
     * @return
     */
    public  boolean put(String key, String value, long outTime) {
//        特判一下
        if (!StringUtils.hasLength(key) || !StringUtils.hasLength(value)) {
            return false;
        }
        try {
            stringRedisTemplate.opsForValue().set(key,value,outTime, TimeUnit.MINUTES);
            return true;
        } catch (Exception e) {
            logger.info("RedisUtil->key:{},value:{} " ,key,value );
            logger.error(",得到的异常为:" + e.getMessage());
            return false;
        }
    }


    /**
     * 从key中获取值
     * @param key
     * @return
     */

    public  String get(String key) {
        // 特判一下
        if (!StringUtils.hasLength(key)) {
            return new String();
        }

        try {
           return stringRedisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            logger.info("RedisUtil->key:{} " ,key );
            logger.error(",得到的异常为:" + e.getMessage());

            return new String();
        }
    }

    /**
     * 查找这个key 是否存储
     * @param key
     * @return
     */
    public  boolean contains(String key) {
        // 特判一下
        if (!StringUtils.hasLength(key)) {
            return false;
        }

        try {
            Boolean aBoolean = stringRedisTemplate.hasKey(key);
            return aBoolean;
        } catch (Exception e) {
            logger.info("RedisUtil->key:{} " ,key );
            logger.error(",得到的异常为:" + e.getMessage());

            return false;
        }
    }

    /**
     * 删除 key-value
     * @param key
     * @return
     */
    public   boolean delete(String key) {
        // 特判一下
        if (!StringUtils.hasLength(key)) {
            return false;
        }

        try {
            Boolean aBoolean = stringRedisTemplate.delete(key);
            return aBoolean;
        } catch (Exception e) {
            logger.error("RedisUtil->key:{}, 得到的异常为：" ,key,e.getMessage());

            return false;
        }
    }


}


